/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     communicate_Modbus.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-11
 * @brief    This file provides UartTFT Application display function
 ********************************************************************************/

#include "communicate_Modbus.h"


#if (UARTBUS_OPTION == 1) || (UARTBUS_OPTION == 2)
extern uint8_t flag_MB_REG[120];

void LT_ModBus_REG_Cmd(void)
{

	uint16_t i = 0;
	for (i = 0; i < sizeof(flag_MB_REG);)
	{
		if (i == 0 && flag_MB_REG[i])
		{
			if (pic_id != (var[2 * VAR_PAGE] << 8 | var[2 * VAR_PAGE + 1]))
			{
				Display_page(var[2 * VAR_PAGE] << 8 | var[2 * VAR_PAGE + 1]);
			}
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 1 && flag_MB_REG[i])
		{
			if (var[VAR_BL * 2 + 1] > 63)
				var[VAR_BL * 2 + 1] = 63;

			LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
			Backlight_flag = 1;

			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 2 && flag_MB_REG[i]) // time
		{
			if (var[VAR_TIME * 2 + 1] > 99)
				var[VAR_TIME * 2 + 1] = 99;
			if (var[(VAR_TIME + 1) * 2 + 1] > 12)
				var[(VAR_TIME + 1) * 2 + 1] = 12;
			if (var[(VAR_TIME + 2) * 2 + 1] > 31)
				var[(VAR_TIME + 2) * 2 + 1] = 31;
			if (var[(VAR_TIME + 3) * 2 + 1] > 23)
				var[(VAR_TIME + 3) * 2 + 1] = 23;
			if (var[(VAR_TIME + 4) * 2 + 1] > 59)
				var[(VAR_TIME + 4) * 2 + 1] = 59;
			if (var[(VAR_TIME + 5) * 2 + 1] > 59)
				var[(VAR_TIME + 5) * 2 + 1] = 59;

			RTC_Set(2000 + var[(VAR_TIME + 0) * 2 + 1], var[(VAR_TIME + 1) * 2 + 1], var[(VAR_TIME + 2) * 2 + 1], var[(VAR_TIME + 3) * 2 + 1], var[(VAR_TIME + 4) * 2 + 1], var[(VAR_TIME + 5) * 2 + 1]);
			flag_MB_REG[i] = 0;
			i += 6;
		}
		else if (i == 8 && flag_MB_REG[i] == 1) // time setting
		{
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 9 && flag_MB_REG[i] == 1) // NULL
		{
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 10 && flag_MB_REG[i] == 1) // WAV
		{
			Wav_DAC_Init((var[VAR_WAV * 2] << 8) | var[VAR_WAV * 2 + 1]);
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 11 && flag_MB_REG[i] == 1) // sound volume
		{
			if (var[2 * VAR_VOLUME + 1] > 16)
				wav_reduce_para = 16;
			else
				wav_reduce_para = var[2 * VAR_VOLUME + 1];
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 12 && flag_MB_REG[i] == 1) // RTP calibration
		{
			if (var[VAR_RTP * 2 + 1] == 0x5A)
			{
#if (LT_TOUCH_FLAG == 1)
				LT_TpAdjust();
#endif
				var[VAR_RTP * 2 + 1] = 0x00;
				Display_page(((var[VAR_PAGE * 2] << 8) | var[VAR_PAGE * 2 + 1]));
			}
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 13 && flag_MB_REG[i] == 1) // Key code trigger
		{
			Button_Key_Code = (var[VAR_KEY * 2] << 8) | var[VAR_KEY * 2 + 1];
			Button_Key_Code_Flag = 1;
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 14 && flag_MB_REG[i] == 1)
		{
			if(var[2 * VAR_BLAUTO + 1] == 1)
			{
				if (Backlight_flag == 0)
				{
					Backlight_count = 0;
					Backlight_flag = 1;
					LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
				}
			}
			else if (var[2 * VAR_BLAUTO + 1] == 0)
			{
				Backlight_count = 0;
				Backlight_flag = 0;
				LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
			}
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 15 && flag_MB_REG[i] == 1)
		{
			if (Backlight_flag == 0 && var[2 * VAR_BLAUTO + 1] == 1)
			{
				LT_BacklightSet(gDutyBuf[var[VAR_BLSLEEP * 2 + 1]]);
			}
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 16 && flag_MB_REG[i] == 1)
		{
			Backlight_time = (var[2 * VAR_BLDURATION] << 8) + var[2 * VAR_BLDURATION + 1];
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 17 && flag_MB_REG[i] == 1)
		{
			if (var[2 * VAR_MCURESET] == 0xAA && var[2 * VAR_MCURESET + 1] == 0x55)
			{
				var[2 * VAR_MCURESET] = 0;
				var[2 * VAR_MCURESET + 1] = 0;

				APP(0x60000000);
			}
			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 0x3F && flag_MB_REG[i] == 1)
		{
			if (var[2 * VAR_MTLANGUAGE + 1] < multi_language_count && var[2 * VAR_MTLANGUAGE + 1] != multi_language)
			{
				uint32_t language_sum = 0,language_count = 0,L_count = 0;
				uint32_t language_addr = 0,L_addr,L_len = 0;
				uint16_t L_num;
				uint8_t rebuf[2100];

				multi_language = var[2 * VAR_MTLANGUAGE + 1];

				LT_ReadFlash_UI(rebuf,addr_index[18] + 8 * multi_language,8);
				language_addr = (rebuf[0]) + (rebuf[1] << 8) + (rebuf[2] << 16) + (rebuf[3] << 24);
				language_sum = (rebuf[4]) + (rebuf[5] << 8) + (rebuf[6] << 16) + (rebuf[7] << 24);

				if (language_sum - language_count >=2048)
						LT_ReadFlash_UI(rebuf,language_addr,2048);
				else
					LT_ReadFlash_UI(rebuf,language_addr,language_sum);

				while(1)
				{
					WDT_FeedDog();
					if (L_count + 4 > 2048)
					{
						if (language_sum - language_count >= 2048)
							LT_ReadFlash_UI(rebuf,language_addr + language_count,2048);
						else
							LT_ReadFlash_UI(rebuf,language_addr + language_count,language_sum - language_count);
						L_count = 0;

					}
					L_addr = rebuf[L_count] + (rebuf[L_count + 1] << 8);
					L_len = rebuf[L_count + 2] + (rebuf[L_count + 3] << 8);
					L_count = L_count + 4;

					if(L_count + L_len > 2048)
					{
						if (language_sum - (language_count + 4) >= 2048)
							LT_ReadFlash_UI(rebuf,language_addr + language_count + 4,2048);
						else
							LT_ReadFlash_UI(rebuf,language_addr + language_count + 4 ,language_sum - (language_count + 4));
						L_count = 0;
					}

					for (uint16_t ll = 0; ll < L_len; ll++)
					{
						var[L_addr * 2 + ll] = rebuf[L_count + ll];
					}
					language_count += L_len + 4;
					L_count += L_len;

					if(language_count >= language_sum)
						break;
				}

				if(Pop_up_flag == 1)
				{
					Display_pop_up(Pop_up_id,Pop_up_x,Pop_up_y);
				}
				else Display_page(pic_id);
			}
			else
				var[2 * VAR_MTLANGUAGE + 1] = multi_language;

			flag_MB_REG[i] = 0;
			i++;
		}
		else if (i == 0x40 && flag_MB_REG[i] == 1)
		{
			if(lt_screen_dir != var[VAR_UI_CHANGE * 2 + 1])
				LT_LCD_screen_dir(var[VAR_UI_CHANGE * 2 + 1]);

			flag_MB_REG[i] = 0;
			i++;
		}
		else
		{
			i++;
		}
	}
}
#endif



